package com.lgqz.himalayafm.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.DrawListener;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.bean.CataIndexBannerEntity;
import com.lgqz.himalayafm.bean.RecyclerRecommend;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:RecyclerView适配器
 * Copyright(c) 2016,邱国兴
 * This Program is protected by copyright laws
 * Created by Little Monster on 2016/12/5.
 *
 * @author 邱国兴
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> implements View
        .OnClickListener {
    private Context context;
    private List<RecyclerRecommend> list;
    private View VIEW_FOOTER;
    private List<CataIndexBannerEntity.CataIndexBannerData> advertisementList;
    private DrawListener listener;
//    private int currentPosition;

    public RecommendAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        advertisementList = new ArrayList<>();
        listener = (DrawListener) context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(viewType, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (!isFooterView(position)) {
            RecyclerRecommend recommend = list.get(position);
            if ("猜你喜欢".equals(recommend.getTitle())) {
                holder.mFourthDraweeView.setImageURI(recommend.getList().get(3).getCoverMiddle());
                holder.mFourthTrackTitle.setText(recommend.getList().get(3).getTrackTitle());
                holder.mFourthTitle.setText(recommend.getList().get(3).getTitle());
                holder.mFiveDraweeView.setImageURI(recommend.getList().get(4).getCoverMiddle());
                holder.mFiveTrackTitle.setText(recommend.getList().get(4).getTrackTitle());
                holder.mFiveTitle.setText(recommend.getList().get(4).getTitle());
                holder.mSixDraweeView.setImageURI(recommend.getList().get(5).getCoverMiddle());
                holder.mSixTrackTitle.setText(recommend.getList().get(5).getTrackTitle());
                holder.mSixTitle.setText(recommend.getList().get(5).getTitle());
            }
            holder.mBigTitle.setText(recommend.getTitle());
            holder.mFirstDraweeView.setImageURI(recommend.getList().get(0).getCoverMiddle());
            holder.mFirstTitle.setText(recommend.getList().get(0).getTitle());
            holder.mFirstTrackTitle.setText(recommend.getList().get(0).getTrackTitle());
            holder.mSecondDraweeView.setImageURI(recommend.getList().get(1).getCoverMiddle());
            holder.mSecondTitle.setText(recommend.getList().get(1).getTitle());
            holder.mSecondTrackTitle.setText(recommend.getList().get(1).getTrackTitle());
            holder.mThirdDraweeView.setImageURI(recommend.getList().get(2).getCoverMiddle());
            holder.mThirdTitle.setText(recommend.getList().get(2).getTitle());
            holder.mThirdTrackTitle.setText(recommend.getList().get(2).getTrackTitle());

            setListener(holder, position, recommend);
        } else {
            if (holder.mViewPager.getChildCount() == 0) {
                bottomAdvertisement(holder);
            }
        }
    }

    private void setListener(ViewHolder holder, int position, RecyclerRecommend recommend) {
//        currentPosition = position;
        if ("猜你喜欢".equals(recommend.getTitle())) {
            holder.mFourthDraweeView.setTag(recommend.getList().get(3).getAlbumId());
            holder.mFiveDraweeView.setTag(recommend.getList().get(3).getAlbumId());
            holder.mSixDraweeView.setTag(recommend.getList().get(3).getAlbumId());

            holder.mFourthDraweeView.setOnClickListener(this);
            holder.mFiveDraweeView.setOnClickListener(this);
            holder.mSixDraweeView.setOnClickListener(this);
        }
        holder.mFirstDraweeView.setTag(recommend.getList().get(0).getAlbumId());
        holder.mSecondDraweeView.setTag(recommend.getList().get(1).getAlbumId());
        holder.mThirdDraweeView.setTag(recommend.getList().get(2).getAlbumId());

        holder.mFirstDraweeView.setOnClickListener(this);
        holder.mSecondDraweeView.setOnClickListener(this);
        holder.mThirdDraweeView.setOnClickListener(this);
    }

    private void bottomAdvertisement(final ViewHolder holder) {
        for (int i = 0; i < advertisementList.size(); i++) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(R.drawable.dot_selector);
            holder.mDotLinearLayout.addView(imageView);
        }
        final ArrayList<SimpleDraweeView> viewList = new ArrayList<>();
        for (int i = 0; i < advertisementList.size(); i++) {
            SimpleDraweeView draweeView = new SimpleDraweeView(context);
            GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
            hierarchy.setPlaceholderImage(R.drawable.ad_default);
            hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            draweeView.setImageURI(advertisementList.get(i).getCover());
            viewList.add(draweeView);
        }
        SimpleDraweeView draweeView = new SimpleDraweeView(context);
        final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
        hierarchy.setPlaceholderImage(R.drawable.ad_default);
        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
        draweeView.setImageURI(advertisementList.get(advertisementList.size() - 1).getCover());
        viewList.add(0, draweeView);

        FocusImageAdapter adapter = new FocusImageAdapter(viewList);
        holder.mViewPager.setAdapter(adapter);
        holder.mViewPager.setCurrentItem(1, false);
        holder.mDotLinearLayout.getChildAt(0).setSelected(true);
        holder.mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int currentPosition;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                for (int i = 0; i < holder.mDotLinearLayout.getChildCount(); i++) {
                    if (i == position - 1) {
                        holder.mDotLinearLayout.getChildAt(i).setSelected(true);
                    } else {
                        holder.mDotLinearLayout.getChildAt(i).setSelected(false);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_IDLE:
                        if (currentPosition == viewList.size() - 1) {
                            holder.mViewPager.setCurrentItem(0, false);
                        } else if (currentPosition == 0) {
                            holder.mViewPager.setCurrentItem(viewList.size() - 1, false);
                        }
                        break;
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (isFooterView(position)) {
            return R.layout.item_advertisement;
        } else if ("猜你喜欢".equals(list.get(position).getTitle())) {
            return R.layout.item_guess;
        } else if ("精品听单".equals(list.get(position).getTitle())) {
            return 0;
        } else {
            return R.layout.item_horizontal;
        }
    }

    @Override
    public int getItemCount() {
        int count = list == null ? 0 : list.size();
        if (VIEW_FOOTER != null) {
            count++;
        }
        return count;
    }

    public void addAll(List<RecyclerRecommend> collection) {
//        list.clear();
        int size = list.size();
//        if (size != 0) {
//            for (int i = 0; i < collection.size(); i++) {
//                Collections.replaceAll(list, list.get(i), collection.get(i));
//            }
//            notifyItemRangeChanged(0, size);
//        }
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
        Log.d("Debug", "addAll: " + list.size());
    }

    public void addFooterView(View footerView) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        footerView.setLayoutParams(params);
        VIEW_FOOTER = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    private boolean isFooterView(int position) {
        return VIEW_FOOTER != null && position == getItemCount() - 1;
    }

    public void getAdvertisementData(List<CataIndexBannerEntity.CataIndexBannerData> data) {
        advertisementList.addAll(data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_first_id:
                Bundle bundle = new Bundle();
                bundle.putLong("albumId", (Long) v.getTag());
                listener.openDraw("热门推荐", bundle);
                break;
            case R.id.item_second_id:
                break;
            case R.id.item_third_id:
                break;
            case R.id.item_fourth_id:
                break;
            case R.id.item_five_id:
                break;
            case R.id.item_six_id:
                break;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mBigTitle;
        private SimpleDraweeView mFirstDraweeView;
        private TextView mFirstTrackTitle;
        private TextView mFirstTitle;
        private SimpleDraweeView mSecondDraweeView;
        private TextView mSecondTrackTitle;
        private TextView mSecondTitle;
        private SimpleDraweeView mThirdDraweeView;
        private TextView mThirdTrackTitle;
        private TextView mThirdTitle;
        private SimpleDraweeView mFourthDraweeView;
        private TextView mFourthTrackTitle;
        private TextView mFourthTitle;
        private SimpleDraweeView mFiveDraweeView;
        private TextView mFiveTrackTitle;
        private TextView mFiveTitle;
        private SimpleDraweeView mSixDraweeView;
        private TextView mSixTrackTitle;
        private TextView mSixTitle;
        private LinearLayout mLinearLayout;
        private ViewPager mViewPager;
        private LinearLayout mDotLinearLayout;

        ViewHolder(View itemView) {
            super(itemView);
            mBigTitle = ((TextView) itemView.findViewById(R.id.item_title_text));
            mFirstDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_first_id));
            mFirstTrackTitle = ((TextView) itemView.findViewById(R.id.item_first_track_title));
            mFirstTitle = ((TextView) itemView.findViewById(R.id.item_first_title));
            mSecondDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_second_id));
            mSecondTrackTitle = ((TextView) itemView.findViewById(R.id.item_second_track_title));
            mSecondTitle = ((TextView) itemView.findViewById(R.id.item_second_title));
            mThirdDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_third_id));
            mThirdTrackTitle = ((TextView) itemView.findViewById(R.id.item_third_track_title));
            mThirdTitle = ((TextView) itemView.findViewById(R.id.item_third_title));

            mFourthDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_fourth_id));
            mFourthTrackTitle = ((TextView) itemView.findViewById(R.id.item_fourth_track_title));
            mFourthTitle = (TextView) itemView.findViewById(R.id.item_fourth_title);
            mFiveDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_five_id));
            mFiveTrackTitle = ((TextView) itemView.findViewById(R.id.item_five_track_title));
            mFiveTitle = ((TextView) itemView.findViewById(R.id.item_five_title));
            mSixDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.item_six_id));
            mSixTrackTitle = ((TextView) itemView.findViewById(R.id.item_six_track_title));
            mSixTitle = ((TextView) itemView.findViewById(R.id.item_six_title));

            mLinearLayout = ((LinearLayout) itemView.findViewById(R.id.ll_more));
            mViewPager = ((ViewPager) itemView.findViewById(R.id.vp_advertisement));
            mDotLinearLayout = ((LinearLayout) itemView.findViewById(R.id.ll_dot_advertisement));
        }
    }
}
